package com.ocm.burnningstar.Adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.ScreenUtils;
import com.ocm.burnningstar.Model.SportUser;
import com.ocm.burnningstar.R;
import com.ocm.burnningstar.Utils.Helper;

import org.xutils.common.Callback;
import org.xutils.image.ImageOptions;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import static com.ocm.burnningstar.GlobalApplication.typeFaceNum;

/**
 * Created by ocm on 2018-01-29.
 */

public class ClassAdapter extends RecyclerView.Adapter<ClassAdapter.ViewHolder> {
    List<SportUser> users = new ArrayList<>();
    private LayoutInflater mInflater;

    public ClassAdapter(Context context, List<SportUser> list){
        mInflater = LayoutInflater.from(context);
        users = list;
    }

    @Override
    public ClassAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.class_item,
                parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        int screenWidth = ScreenUtils.getScreenWidth();
        int screenHeight = (int)(ScreenUtils.getScreenHeight()*1.2);
        int viewWidth = screenWidth/5;
        int viewHeight = (int)(screenHeight*0.25);
//        AbsListView.LayoutParams params = new AbsListView.LayoutParams(viewWidth,viewHeight);
//        view.setLayoutParams(params);

        viewHolder.header = (CircleImageView) view.findViewById(R.id.header);
//        viewHolder.name = (TextView) view.findViewById(R.id.name);
        viewHolder.strength = (TextView) view.findViewById(R.id.strength);
        viewHolder.heartRate = (TextView) view.findViewById(R.id.heartRate);
        viewHolder.calorie = (TextView) view.findViewById(R.id.calorie);
        viewHolder.courseBack =  (LinearLayout) view.findViewById(R.id.courseBack);
        viewHolder.strength.setTypeface(typeFaceNum);
        viewHolder.heartRate.setTypeface(typeFaceNum);
        viewHolder.calorie.setTypeface(typeFaceNum);

//        ViewGroup.LayoutParams hs = viewHolder.header.getLayoutParams();
//        ViewGroup.LayoutParams ns = viewHolder.name.getLayoutParams();
//        ns.width = hs.width =hs.height = (int)(viewHeight*0.5);
//        viewHolder.header.setLayoutParams(hs);
//        viewHolder.name.setLayoutParams(ns);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ClassAdapter.ViewHolder holder, int position) {
        SportUser user = users.get(position);
//        holder.header.setImageBitmap(user.getHeader());
        ImageOptions imageOptions= new ImageOptions.Builder().setFadeIn(true).build();
        x.image().loadDrawable(user.getHeaderStr(), imageOptions, new Callback.CacheCallback<Drawable>() {
            @Override
            public boolean onCache(Drawable result) {
                holder.header.setImageBitmap(Helper.drawableToBitmap(result));
                return true;
            }

            @Override
            public void onSuccess(Drawable result) {
                Log.i("onBindViewHolder","onSuccess");
                holder.header.setImageBitmap(Helper.drawableToBitmap(result));
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i("onBindViewHolder","onError");
                ex.printStackTrace();
            }
            @Override
            public void onCancelled(CancelledException cex) {
                Log.i("onBindViewHolder","onCancelled");
                cex.printStackTrace();
            }
            @Override
            public void onFinished() {            }
        });
//        holder.name.setText(user.getName());
        holder.strength.setText(user.getStrength());
        holder.calorie.setText(user.getCalorie());
        holder.heartRate.setText(user.getHeartRate());
        holder.courseBack.setBackgroundResource (user.getResource());
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return users.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder
    {
        public ViewHolder(View arg0)
        {
            super(arg0);
        }
        CircleImageView header;
        TextView name;
        TextView heartRate;
        TextView calorie ;
        TextView strength;
        LinearLayout courseBack;
    }

}
